import ApiWorkflow from "@/api/workflow";

import useList from "./useList";
import useFetch, { UseFetchOptions } from "./useFetch";

import { WorkflowDefinition } from "@/types/work";

export default (
  params?: { typeCode?: string; definitionCode?: string },
  fetchOption?: Pick<UseFetchOptions<any, any[]>, "onSuccess">
) => {

  const { list } = useList<WorkflowDefinition>(ApiWorkflow.workFlowDefinition, {
    defaultParams: [params],
  });

  const { loading, run } = useFetch(ApiWorkflow.submit, "POST", {
    ...fetchOption,
    manual: true,
  });

  const bind = (
    payload: {
      businessId: string;
      intentCustomerId: number;

      typeCode?: string;
      workFlowDefinitionId?: number;
    },
    index = 0
  ) => {
    const data = list[index];
    run({
      typeCode: params?.typeCode,
      workFlowDefinitionId: data?.workflowDefinitionId,
      ...payload,
    });
  };

  return [list, { loading, bind }] as const;
};
